package com.kgc.personprovider.controller;

import com.kgc.basic.util.ResponseMsg;
import com.kgc.personcommon.pojo.User;
import com.kgc.personcommon.remote.UserRemote;
import com.kgc.personprovider.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

@Controller
public class UserController implements UserRemote {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserService userService;

    @Override
    public ResponseMsg getUserPage(Map<String, Object> params) {
        logger.info("UserController getUserPage params={}", params);
        ResponseMsg resp = userService.getByPage(params);
        logger.debug("userService.getByPage → {}", resp);
        return resp;
    }

    @Override
    public ResponseMsg addUser(User user) {
        logger.info("UserController addUser user={}", user);
        ResponseMsg resp = userService.add(user);
        logger.debug("userService.add → {}", resp);
        return resp;
    }

    @Override
    public ResponseMsg updateUser(User user) {
        logger.info("UserController updateUser user={}", user);
        ResponseMsg resp = userService.update(user);
        logger.debug("userService.update → {}", resp);
        return resp;
    }

    @Override
    public ResponseMsg delUser(Integer id) {
        logger.info("UserController delUser id={}", id);
        ResponseMsg resp = userService.delete(id.toString());
        logger.debug("userService.delete → {}", resp);
        return resp;
    }

    @Override
    public ResponseMsg getUserById(Integer id) {
        logger.info("UserController getUserById id={}", id);
        ResponseMsg resp = userService.getById(id.toString());
        logger.debug("userService.getById → {}", resp);
        return resp;
    }

    @Override
    public ResponseMsg getAllUser(Map<String, Object> params) {
        logger.info("UserController getAllUser params={}", params);
        ResponseMsg resp = userService.getAll(params);
        logger.debug("userService.getAll → {}", resp);
        return resp;
    }

    @Override
    public ResponseMsg login(@RequestParam("account") String account, @RequestParam("password") String password, @RequestParam("checkCode") String checkCode) {
        logger.info("provider UserController login account:{},password:{},checkCode:{}", account, password, checkCode);
        ResponseMsg msg = userService.login(account, password, checkCode);
        logger.debug("provider UserController login msg:{}", msg);
        return msg;
    }

    @Override
    public String getCaptcha() {
        logger.info("provider getCaptcha is start...");
        String s = userService.getCaptcha();
        logger.debug("provider getCaptcha s:{}", s);
        return s;
    }

    @Override
    public ResponseMsg logout(@RequestBody Map<String, String> request) {
        logger.info("provider logout is start...");
        String sessionId = request.get("sessionId");
        if (sessionId == null || sessionId.isEmpty()) {
            ResponseMsg msg = new ResponseMsg();
            msg.setCode("400");
            msg.setMsg("缺少sessionId参数");
            return msg;
        }
        ResponseMsg msg = userService.logout(sessionId);
        logger.debug("provider logout msg:{}", msg);
        return msg;
    }
    @Override
    public ResponseMsg getTreePermissionsByUserId(@RequestParam("account") String account) {
        logger.info("UserController getTreePermissionsByUserId is start...");
        ResponseMsg responseMsg =userService.getTreePermissionsByUserId(account);
        logger.debug("userService getTreePermissionsByUserId:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg refresh(Map<String, String> map) {
        logger.info("UserController getTreePermissionsByUserId is start...");
        ResponseMsg responseMsg =userService.refresh(map);
        logger.debug("userService getTreePermissionsByUserId:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg selectUserByMap(Map<String, Object> map) {
        logger.info("UserController selectUserByMap is start... map{}" , map);
        ResponseMsg resp = userService.selectUserByMap(map);
        logger.debug("userService selectUserByMap:{} ,response{}", map , resp);
        return resp;
    }

}
